package com.iwuke.dance.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.iwuke.dance.activity.LPWHApplication;

public class SharedPreferencesUtil {

    public String fileName = "fileName";
    public String token="TOKEN";

    public SharedPreferences sharedPreferences;
    public SharedPreferences.Editor editor;

    private static SharedPreferencesUtil instance;

    public static synchronized SharedPreferencesUtil getInstance() {
        if (instance == null) {
            instance = new SharedPreferencesUtil();
        }
        return instance;
    }

    //步骤1：创建一个SharedPreferences对象
    public SharedPreferencesUtil() {
        /*Context.MODE_PRIVATE: 指定该SharedPreferences数据只能被本应用程序读、写；
        Context.MODE_WORLD_READABLE:  指定该SharedPreferences数据能被其他应用程序读，但不能写；
        Context.MODE_WORLD_WRITEABLE:  指定该SharedPreferences数据能被其他应用程序读；
        Context.MODE_APPEND：该模式会检查文件是否存在，存在就往文件追加内容，否则就创建新文件；*/
        if (sharedPreferences == null)
            sharedPreferences = LPWHApplication.getContext().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        if (editor == null)
            editor = sharedPreferences.edit();

    }


    public static synchronized SharedPreferencesUtil getPrintStatus() {
        if (instance == null) {
            instance = new SharedPreferencesUtil("printStatus");
        }
        return instance;
    }

    public SharedPreferencesUtil(String name) {//printStatus 打印状态"printStatus"
        if (sharedPreferences == null)
            sharedPreferences = LPWHApplication.getContext().getSharedPreferences(name, Context.MODE_PRIVATE);
        if (editor == null)
            editor = sharedPreferences.edit();

    }

    //保存ToKen
    public void setToKen(String value){
        editor.putString(token,value).commit();
    }
    //得到设备id
    public String getToKen(String key){
        return sharedPreferences.getString(key,"");
    }

    public void setWalletId(String walletId){
        editor.putString("walletId",walletId).commit();
    }

    public String getWalletId(){
        return sharedPreferences.getString("walletId","1955891278185435136");
    }

    public void setUserId(String userId){
        editor.putString("userId",userId).commit();
    }

    public String getUserId(){
        return sharedPreferences.getString("userId","123123131");
    }

    public void setPhoneNumber(String phoneNumber){
        editor.putString("phoneNumber",phoneNumber).commit();
    }

    public String getPhoneNumber(){
        return sharedPreferences.getString("phoneNumber","18511698580");
    }

    //移除指定数据
    public void clearCache(String key) {
        editor.remove(key).commit();
    }

    //删除全部数据all Cache
    public void clearAllCache() {
        editor.clear().commit();
    }

}
